/**
 * Copyright ©2003~2019年4月7日 szkingdom
 * @author raohj
 * @date 2019年4月7日
 * @file RDOM.java
 * @comment : please input your comment to this file
 * @version : input version
 * @modify:
 * 		1、 2019年4月7日 由 raohj 新建文件
 */
package com.ccnu.demojava.xml;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

/**
 * @author raohj
 * @date 2019年4月7日
 * @version 
 * @comment to add your comment
 */
public class RDOM {
	public static final String XML_PATH = "D:/temp/tmp/1.xml";
	private static Logger log = LoggerFactory.getLogger(RDOM.class);
	
	public static void DOM() {
		DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = null;
		try {
			builder = builderFactory.newDocumentBuilder();
			File f = new File(XML_PATH);
			BufferedInputStream bis = new BufferedInputStream(Files.newInputStream(f.toPath()));

			Document rootDoc= builder.parse(bis);
			log.info("XML-version:" +  rootDoc.getXmlVersion() + rootDoc.getXmlEncoding());
			log.info("XML-NodeName:" +  rootDoc.getNodeName());
			
			// 获取第一个子节点
			NodeList nodeL = rootDoc.getChildNodes();
			Node contentN = nodeL.item(0);
			log.info("XML-NodeName:" +  contentN.getFirstChild().getNodeName() + "|text:" + contentN.getFirstChild().getTextContent());
			
			// 获取Head节点
			Node headN = contentN.getFirstChild();
			NodeList headList = headN.getChildNodes();
			for(int i=0; i<headList.getLength(); i++) {
				log.info(headList.item(i).getNodeValue());		
			}
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
